package com.faithbj.controller.statistics;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.faithbj.common.configuration.Pager;
import com.faithbj.common.utils.DateUtils;
import com.faithbj.model.conditions.InfoSearchCondition;
import com.faithbj.service.statistics.SearchService;

/**
 * 核对业务查询：基本信息查询页面controller
 * @author lihao
 *
 */
@Controller
@RequestMapping("/searchInfo")
public class SearchController extends StatBaseController {
	
	@Resource
	SearchService searchService;
	
	
	@RequestMapping("index") 
	public String index() {
		return "/analysis/index";
	}
	
	@RequestMapping("/queryBaseInfo")
	public String queryBaseInfo(HttpServletRequest request, ModelMap model) {
		//初始化区县列表
		this.initCountryRegionList();
		
		//当type为1时表示从菜单栏跳转过来，需要初始化时间段区间
		InfoSearchCondition condition = new InfoSearchCondition();
		if("1".equals(request.getParameter("type"))){
			Date endDate = new Date();
			condition.setEndTime(endDate);
			condition.setStartTime(this.getStartTime(endDate));
		}
		condition.setAcceptNumber(request.getParameter("accept_number"));
		condition.setRegionIdOfCounty(request.getParameter("country"));
		condition.setRegionIdOfStreet(request.getParameter("street"));
		condition.setAuthorizeNumber(request.getParameter("authorized_num"));
		condition.setApplicantName(request.getParameter("applicant_name"));
		String startTime = request.getParameter("accept_start_time");
		if(StringUtils.isNotBlank(startTime)) {
			startTime = this.filterStartTime(startTime);
			condition.setStartTime(DateUtils.getStringToDate(startTime, DateUtils.DateFormatDefault));
		}
		String endTime = request.getParameter("accept_end_time");
		if(StringUtils.isNotBlank(endTime)) {
			endTime = filterEndTime(endTime);
			condition.setEndTime(DateUtils.getStringToDate(endTime, DateUtils.DateFormatDefault));
		}
		condition.setIdentity(request.getParameter("identity"));
		condition.setCheckState(request.getParameter("check_state"));
		
		Pager pager = new Pager();
		Integer pageNumber = 1;
		if(request.getParameter("page_number") != null){
			pageNumber = Integer.valueOf(request.getParameter("page_number")).intValue();
		}
		pager.setPageNumber(pageNumber);
		condition.setPager(pager);
		
		//查询
		Map map = searchService.queryBaseInfo(condition);
		model.put("resultList", map.get("resultList"));
		model.put("pager", map.get("pager"));
		model.put("countryRegionList", this.countryRegionList);
		
		return "/analysis/queryBaseInfo";
	}
}
